/**
 * sdfactory
 */

function handleFiles(files) {

	if (files.length) {
		var file = files[0];
		var reader = new FileReader();

		if (file.type == "" || /text\/\w+/.test(file.type)) {
			reader.onload = function() {
				$("#result").html("");
				window.sendData = $("#result").codeArea(this.result);
				// $('<pre>' + this.result + '</pre>').appendTo('body');
			};
			reader.readAsText(file);
		}

	}
}

// a function create table
function createTable(data) {
	debugger
	$("#tableArea").html("");
	window.ResultJson = data;
	$.each(data, function(counter) {
		var div = "<div style='border:1px solid;width:100%'>" + "<table><tr><td><input type=\"checkbox\" id='" + counter + "checkbox' /></td>" + "<td><div class='label'>" + this.v2.fromLssServer + "</div></td></tr></table>" + "<div id='" + counter + "detail' style='display:none'>" +
		// "<table style='width:100%'><tr><td style='width:20%'><div
		// class='label'>sensor
		// Annotation:</div></td><td>"+this.v2.sensorAnnotation+"</td></tr></table>"+
		"<table style='width:100%'><tr><td style='width:20%'><div class='label'>sensor uri:</div></td><td>" + this.v2.sensorUri + "</td></tr></table>" + "<table style='width:100%'><tr><td style='width:20%'><div class='label'>sensor Annotation:</div></td><td><textarea id='" + counter
				+ "newlyCreatedAnnotation' style='width:90%'></textarea></td></tr></table>" + "<div style='text-align:center'><input type='button' value='submit' id='" + counter + "button' /></div>" + "</div>" + "</div>";

		$("#tableArea").append(div);
		$("#" + counter + "button").button().click(function() {
			var server = $("#Service").val();
			var sendData = new Object();
			sendData.newlyCreatedAnnotation = $("#" + counter + "newlyCreatedAnnotation").val();
			sendData.feedback = new Array();
			sendData.feedback[0] = new Object();
			sendData.feedback[0].sensorUri = ResultJson[counter].v2.sensorUri;
			sendData.feedback[0].isUseful = true;
			sendData.feedback[0].similarityScore = ResultJson[counter].v1;
			debugger
			$.ajax({
				type : "post",
				data : sendData,
				url : server + "/search/sss/feedback",
				success : function(data) {
					debugger
				},
				error : function() {
					debugger
				}
			});
		});
		$("#" + counter + "checkbox").click(function() {
			$("#" + counter + "detail").slideToggle();
		});
	});

}

$(function() {
	$("#uploadButton").button().click(function() {

		var server = $("#Service").val();
		var number = $("#number").val();
		try {
			var data = window.sendData.data;
		} catch (e) {
			alert("please choose one senser file");
		}

		if (server == null || server == "") {
			alert("please typing the server location");
			return false;
		}

		if (number == null || number == "") {
			alert("please typing the limit number ");
			return false;
		}

		// use ajax get the returnjson
		var returnJson;

		/*
		 * $.ajax({ async : false, url : '../rankList.result', success :
		 * function(data) { jsonArray = data.split('\n'); DataArray = new
		 * Array(); $.each(jsonArray, function() { if (this != "") {
		 * DataArray.push($.parseJSON(this)); }
		 * 
		 * });
		 * 
		 * returnJson=DataArray; // alert(DataArray.length); }, error :
		 * function() { } });
		 */
		debugger;
		$.ajax({
			type : "post",
			url : "http://localhost:1180/search/sss/10",
			data : data,//data from 91128-1.sensor
			success : function(data) {
				debugger
				alert(data);
			},
			error : function() {
				debugger
				alert("error");
			}
		});

		// create table
		createTable(returnJson);

	});

});